/**
 * 
 */
package fr.isima.isipaint.controllers;

import java.awt.event.MouseEvent;

import javax.swing.event.MouseInputListener;

import fr.isima.isipaint.model.GraphicGroup;
import fr.isima.isipaint.model.GraphicObject;
import fr.isima.isipaint.model.Scene;

/**
 * @author daniel
 * 
 */
public class DrawPanelObjectController implements MouseInputListener {

	/** The model */
	private Scene scene;

	/* The difference between mouse position and the top-l */
	/** The last X coordinate of the mousse */
	private int xDiff;

	/** The last Y coordinate of the mouse */
	private int yDiff;

	public DrawPanelObjectController(Scene scene) {
		this.scene = scene;
	}

	@Override
	public void mouseDragged(MouseEvent e) {

		if (scene.isSelection()) {
			if (scene.getCurrent() != null) {
				scene.setLocationGraphicObject(scene.getCurrent(), e.getX()
						- xDiff, e.getY() - yDiff);

				scene
						.setBoundsGraphicObject(scene.getSelectionBox(), scene
								.getCurrent().getX() - 1, scene.getCurrent()
								.getY() - 1, scene.getCurrent().getWidth() + 2,
								scene.getCurrent().getHeight());
			} else {

				scene.setBoundsGraphicObject(scene.getSelectionBox(), scene
						.getSelectionBox().getX(), scene.getSelectionBox()
						.getY(), e.getX() - scene.getSelectionBox().getX(), e
						.getY()
						- scene.getSelectionBox().getY());
				System.out.println(scene.getSelectionBox());
			}
		} else if (scene.isDrawAction()) {
			if (scene.getCurrent() != null) {

				scene.setBoundsGraphicObject(scene.getCurrent(), scene
						.getCurrent().getX(), scene.getCurrent().getY(), e
						.getX()
						- scene.getCurrent().getX(), e.getY()
						- scene.getCurrent().getY());
			}
		}

	}

	@Override
	public void mouseMoved(MouseEvent e) {

	}

	@Override
	public void mouseClicked(MouseEvent e) {

		System.out.println(e);

		if (scene.isFilledAction()) {
			System.out.println("Fill");
			GraphicObject selected = scene.getGraphicObject(e.getX(), e.getY());
			if (selected != null) {
				scene.setCurrent(selected);
				scene.fillCurrent();
			}
			selected.setFilled(true);
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		
		
		if (scene.isSelection()) {
			GraphicObject selected = scene.getGraphicObject(e.getX(), e.getY());
			//One item
			if (selected != null) {
				xDiff = e.getX() - selected.getX();
				yDiff = e.getY() - selected.getY();
				scene.setCurrent(selected);
			} 
			//A Group
			else {
				scene.setLocationGraphicObject(scene.getSelectionBox(), e
						.getX(), e.getY());
			}

		} else if (scene.isDrawAction()) {

			if (scene.getCurrent() != null) {
				GraphicObject current = (GraphicObject) scene.getCurrent();
				scene.addGraphicObject(current);
				scene.setLocationGraphicObject(scene.getCurrent(), e.getX(), e
						.getY());
			} else {
				scene.setCurrent(null);
			}
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {

		if (scene.isSelection()) {
			if (scene.getCurrent() != null) {
				scene.setCurrent(null);
				scene.resetSelection();
			} else {
				GraphicGroup box = scene.getGroup().getBox(
						scene.getSelectionBox().getBounds());
				if (box != null) {
					scene.setGroupBox(box);
					scene.setBoundsGraphicObject(scene.getSelectionBox(), box
							.getX(), box.getY(), box.getWidth(), box
							.getHeight());
				} else {
					scene.resetSelection();
				}
			}
		} else {
			if (scene.getCurrent() != null) {
				scene.resetCurrent();
			}
		}
	}

}
